package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
import java.util.Random;

/**
 * 磊哥 写的代码雨
 */
public class Rain extends JDialog implements ActionListener {

    /**
     * 整个屏幕的尺寸
     */
    private Dimension screenSize;

    /**
     * 准备一支画笔
     */
    private JPanel glPanel;

    /**
     * 设置行高和行宽
     */
    private final static int GAP = 20;

    /**
     * 行数
     */
    private int lines;

    /**
     * 列数
     */
    private int columns;

    /**
     * 雨点开始位置坐标数组
     */
    private int[] posArr;

    /**
     * 提供给类内使用一个随机数对象
     */
    private Random random = new Random();

    public Rain() {
        initComponents();
    }
    /**
     * 随机获取字符常量
     *
     * @return
     */
    private char getChar() {
        return (char) (random.nextInt(94) + 33);
    }

    /**
     * 按键监听器，用户按键ESC退出程序
     */
    private class KeyPressListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ESCAPE == e.getKeyCode()) {
                System.exit(0);
            }
        }
    }

    private class GraphicPanel extends JPanel {
        @Override
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;

            // 设置字体
            g2d.setFont(getFont().deriveFont(Font.BOLD));
            // 背景演示黑色
            g2d.setColor(Color.BLACK);
            // 整体界面填充
            g2d.fillRect(0, 0, screenSize.width, screenSize.height);

            // 当前列数据确定
            int currentColumn = 0;
            for (int x = 0; x < screenSize.width; x += GAP) {
                int endPos = posArr[currentColumn];
                // 那笔画
                g2d.setColor(Color.CYAN);
                // 使用画笔画出字符
                g2d.drawString(getChar() + "", x, endPos * GAP);

                int colorChange = 0;
                for (int i = endPos - 15; i < endPos; i++) {
                    colorChange += 20;

                    if (colorChange > 255) {
                        colorChange = 255;
                    }

                    // 设置当前也字体颜色，RGB模式 只选择控制GREEN
                    g2d.setColor(new Color(0, colorChange, 0));
                    g2d.drawString(getChar() + "", x, i * GAP);
                }

                posArr[currentColumn] += random.nextInt(5);

                if (posArr[currentColumn] * GAP > getHeight()) {
                    posArr[currentColumn] = random.nextInt(lines);
                }

                currentColumn += 1;
            }
        }
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        glPanel = new GraphicPanel();
        add(glPanel, BorderLayout.CENTER);

        // 设置光标不可见
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Image image = defaultToolkit.createImage(new MemoryImageSource(0, 0, null, 0, 0));
        Cursor customCursor = defaultToolkit.createCustomCursor(image, new Point(0, 0), "Cursor");
        setCursor(customCursor);

        // 添加ESC退出监听
        KeyPressListener keyPressListener = new KeyPressListener();
        this.addKeyListener(keyPressListener);

        // 取出标题栏
        this.setUndecorated(true);

        // 全屏方式
        this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(true);

        // 当前整个屏幕的尺寸
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        lines = screenSize.height / GAP;
        columns = screenSize.width / GAP;

        // 给予坐标数组随机数据
        posArr = new int[columns +  1];
        for (int i = 0; i < posArr.length; i++) {
            posArr[i] = random.nextInt(lines);
        }

        // 10帧一秒
        new Timer(100, this).start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        glPanel.repaint();
    }

    public static void main(String[] args) {

        new Rain();
    }
}
